﻿using Tester.Models;
using Tester.Views;
#if ANDROID

#endif

namespace Tester.ViewModels;

internal class MainViewModels : IViewModels
{
    public List<IWidget> Widgets { get; set; }
    public CommonConfig CommonConfig { get; set; }

    public MainViewModels()
    {
        Widgets = new List<IWidget>();
        InitializeWidget();
        CommonConfig = new();
        try
        {
            CommonConfig = Models.ReadAndWrite.Deserialize(Models.ReadAndWrite.FileRead());
        }
        catch (Exception ex)
        {
            DisplayAlert("提示", "未找到配置文件或配置文件不正确。\n将进行初始化。\n错误讯息:"+ex.Message, "OK");
            InitConfig();
        }
        if (CommonConfig == null)
        {
            DisplayAlert("提示", "未找到配置文件或配置文件不正确。\n将进行初始化。", "OK");
            InitConfig();
        }
    }

    public void InitializeWidget()
    {
        Widgets.Add(new CommonWidget{ Header = "测试", Value1 = 0, Mode = 0, Description = "\ue72e Execute", EditButtonCommand = new Command(OnEdit) });
        Widgets.Add(new CommonWidget { Header = "测试2", Value1 = 0, Mode = 0, Description = "\ue72e Execute", EditButtonCommand = new Command(Test) });
        Widgets.Add(new CommonWidget { Header = "测试3", Value1 = 0, Mode = 0, Description = "\ue72e Execute", EditButtonCommand = new Command(GetStorage) });
        Widgets.Add(new CommonWidget { Header = "导航测试", Value1 = 0, Mode = 0, Description = "\ue72e Execute", EditButtonCommand = new Command(Navito) });
    }

    private void OnEdit()
    {
        string info = string.Empty;
        info += "CurrentDirectory:" + Environment.CurrentDirectory + "\n";
        info += "ProcessPath:" + Environment.ProcessPath + "\n";
        info += "Personal:" + Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\n";
        Models.Message.Show("ShowDialog", "title", info, "ok");
    }

    private void Test()
    {
        Models.Message.Execute("ResizeWindow");
    }

    private void GetStorage()
    {
        Models.Message.Show("ShowDialog", "title", Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "OK");
    }

//    private async void WriteSomething()
//    {
//#if ANDROID
//        PermissionStatus statusread = await Permissions.RequestAsync<Permissions.StorageRead>();
//        PermissionStatus statuswrite = await Permissions.RequestAsync<Permissions.StorageWrite>();
//        string path = "/storage/emulated/0/Tester";
//        Directory.CreateDirectory(path);
//        FileStream fs = new(path + "/test.json", FileMode.Create);
//        StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
//        await sw.WriteAsync(str);
//        sw.Close();
//        fs.Close();
//#elif WINDOWS
//        string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\test.json";
//        FileStream fs = new(path, FileMode.Create);
//        StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
//        await sw.WriteAsync(str);
//        sw.Close();
//        fs.Close();
//#endif
//    }

    private void Navito()
    {
        Navigate.Execute("Navigate", typeof(Tester.Views.PersonalizePage));
    }

    private void InitConfig()
    {
        CommonConfig = new();
        CommonConfig.themes.Add("跟随系统");
        CommonConfig.themes.Add("浅色");
        CommonConfig.themes.Add("深色");
        Models.ReadAndWrite.Write(CommonConfig);
        try
        {
            CommonConfig = Models.ReadAndWrite.Deserialize(Models.ReadAndWrite.FileRead());
        }
        catch (Exception ex)
        {
            DisplayAlert("提示", "配置文件不正确。", "OK");
            throw new Exception();
        }
    }

    private void DisplayAlert(string title, string details, string accept)
    {
        Models.Message.Show("ShowDialog", title, details, accept);
    }
}